var flag=0;
var pageNo=1;
var pageSize=30;
var beginTime="";
var endTime="";
var pageNum;
var tempFlag=1;
var today = Date.parse(new Date());
$("#pageSizeInfo").val("30");

function exportAllOrder(){
    beginTime=$("#startTime").val();
    endTime=$("#endTime").val();
    var searchName=$("#searchCondition").val();
    location.href = web_url_prefix + "userAccess/exportPageStatistics?startDate=" + beginTime +
        "&endDate=" + endTime+ "&searchName=" + searchName
}

function exportSelect(){
    var checkedLength = 0;
    $(".i-checks").each(function (i, d) {
        if ($(d).children().hasClass("checked")) {
            checkedLength++
        }
    });
    if (checkedLength == 0) {
        parent.layer.msg("请勾选");
        return;

    }
    $("tbody").find("tr").each(function (i, d) {
        $(d).addClass("noExl")
    });
    $(".i-checks").each(function (i, d) {
        if ($(d).children().hasClass("checked")) {
            $(d).parent().parent().removeClass("noExl")
        }
    });
    $("#table").table2excel({
        // 不被导出的表格行的CSS class类
        exclude: ".noExl",
        // 导出的Excel文档的名称，（没看到作用）
        name: "Excel Document Name",
        // Excel文件的名称
        filename: "流量报表"
    });
}

//创建页码信息
function createPageInfo(data) {
    $("#startRow").text(data.startRow);
    $("#endRow").text(data.endRow);
    $("#totalPage").text(data.total);
    pageNum=data.pages;
    var temp = '';
    for (var i = 1; i < data.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageInfo").html(temp)
        .val(data.pageNum)
        .change(function () {
            if (flag === 1) {
                return
            }
            pageNo = this.options[this.options.selectedIndex].value;
            showList();
        });
    $("#firstPage").click(function () {
        if (pageNo!== 1) {
            if (flag === 1) {
                return
            }
            pageNo = 1;
            showList();
        }
    });
    $("#lastPage").click(function () {
        if (pageNo < pageNum) {
            if (flag === 1) {
                return
            }
            pageNo = pageNum;
            showList();
        }
    });
    $("#nextPage").click(function () {
        if (pageNo < pageNum) {
            if (flag === 1) {
                return
            }
            pageNo++;
            showList();
        }
    });
    $("#previousPage").click(function () {
        if (pageNo > 1) {
            if (flag === 1) {
                return
            }
            pageNo--;
            showList();
        }
    });
    //改变页面显示数量
    $("#pageSizeInfo").change(function () {
        $("#pageSizeInfo").blur();
        if (flag === 1) {
            return
        }
        pageSize = this.options[this.options.selectedIndex].value;
        pageNo=1;
        showList();
    });
}

//table 的ajax请求
function showList(){
    if(flag!==0){
        return;
    }
    flag=1;
    var beginTime=$("#startTime").val();
    var endTime=$("#endTime").val();
    var searchName=$("#searchCondition").val();
    ajax({
        url:"userAccess/pageStatistics",
        data:{
            startDate:beginTime,
            endDate:endTime,
            searchName:searchName,
            pageNum:pageNo,
            pageSize:pageSize
        },
        success:function(data){
            if(data.success){
                
                flag=0;
                temp(data.data);
                createPageInfo(data.data.statistics);
                $("#checkAll").html("全选");
            }else{
                layer.msg(data.message)
            }
        }
    });
}

//请求成功后执行的table列表
function temp(data){
    document.getElementById('tbody').innerHTML = template("template",data.statistics);
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
    if(tempFlag===1){
        showData(data.summary);
    }
}

//展示总数据
function showData(data){
    $("#pv").html(data.pv);
    $("#uv").html(data.uv);
    $("#ip").html(data.ip);
    $("#averagePv").html(data.averagePv);
    $("#outPv").html(data.outPv);
    $("#averageTime").html(data.averageTime);
}

//设置时间
function setReportTime(index) {
    var startTime=null;
    var endTime=null;
    switch (index){
        case 0:
            startTime=timeStamp2String(Number.parseFloat(today)-2592000000,"ymd")+" 00:00:00";
            endTime=timeStamp2String(Number.parseFloat(today)-86400000,"ymd")+" 23:59:59";
            break;
        case 1:
            startTime=timeStamp2String(Number.parseFloat(today)-604800000,"ymd")+" 00:00:00";
            endTime=timeStamp2String(Number.parseFloat(today)-86400000,"ymd")+" 23:59:59";
            break;
        case 2:
            startTime=timeStamp2String(Number.parseFloat(today)-86400000,"ymd")+" 00:00:00";
            endTime=timeStamp2String(Number.parseFloat(today)-86400000,"ymd")+" 23:59:59";
            break;
        case 3:
            startTime=timeStamp2String(today, "ymd")+" 00:00:00";
            endTime=timeStamp2String(today, "ymdhms");
            break;
    }
    $("#startTime").val(startTime);
    $("#endTime").val(endTime);
    showList();
}

//时间选择
function datePicker(){
    laydate.render({
        elem: '#endTime'
        , max: today
        ,showBottom: true
        ,value:timeStamp2String(today, "ymdhms")
        , type: 'datetime'
        , theme: 'molv'
    });
    laydate.render({
        elem: '#startTime'
        , max: today
        ,showBottom: true
        , theme: 'molv'
        , type: 'datetime'
        ,value:timeStamp2String(today, "ymd")+" 00:00:00"
    });
    showList();
}
//基本操作
function handle(){
    //全选/全不选
    $("#checkAll").click(function () {
        if ($(this).text() == "全选") {
            $("#tbody").find(".i-checks").iCheck('check');
            $(this).text("不选")
        } else {
            $("#tbody").find(".i-checks").iCheck('uncheck');
            $(this).text("全选")
        }
    });
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
    $(".dateTime").click(function () {
        var index=$(this).index(".dateTime");
        $(".dateTime").removeClass("btn-info");
        $(this).addClass("btn-info");
        setReportTime(index);
    });
    $("#tbody").on("click",".detail",function(){
        var searchName=$(this).attr("data-search");
        setStorage("searchName",searchName);
        window.location.href="interviewPage.html";
    });
    $("#triggerCheck").on("ifClicked",function(){
        $("#checkAll").trigger("click");
    });
    $("#inquire").click(function () {
        showList();
    });
    $("#exportAll").click(function(){
        exportAllOrder();
    });
    $("#exportSelect").click(function(){
        exportSelect();
    })
}
$(document).ready(function () {
    datePicker();
    handle();
});